package com.meizu.asum.richtextsdk.activity.designer;

import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.meizu.asum.richtextsdk.R;
import com.meizu.asum.richtextsdk.adapter.ViewPagerAdapter;
import com.meizu.asum.richtextsdk.config.RichTextAlignment;
import com.meizu.asum.richtextsdk.table.FlymeBBSEmoticonIconTable;
import com.meizu.asum.richtextsdk.uiframework.xlayoutparam.utils.XPxArea;
import com.meizu.asum.richtextsdk.uiframework.xuidesign.utils.BaseActivityDesigner;
import com.meizu.asum.richtextsdk.view.RichEditText;

import java.util.ArrayList;

import cn.dreamtobe.kpswitch.widget.KPSwitchPanelRelativeLayout;

/**
 * Description： <br>
 * User：Xie JunWen <br>
 * Email：v-xiejunwen@meizu.com <br>
 * CreateTime：2018/3/20  16:42 <br>
 * Remark：无
 */
public class MainActivityDesigner extends BaseActivityDesigner {
    public RichEditText editText;

    public LinearLayout functionLayout;
    public Button emoticonButton;
    public Button picButton;
    public Button topicButton;

    public KPSwitchPanelRelativeLayout panelLayout;
    public ViewPager viewPager;
    public ArrayList<ImageView> emoticonImageViews;

    @Override
    protected void getWidgets() {
        editText = designer.getViewById(R.id.activity_main_edittext);

        functionLayout = designer.getViewById(R.id.activity_main_function_layout);
        emoticonButton = designer.getViewById(R.id.activity_main_function_emoticon_button);
        picButton = designer.getViewById(R.id.activity_main_function_pic_button);
        topicButton = designer.getViewById(R.id.activity_main_function_topic_button);

        panelLayout = designer.getViewById(R.id.activity_main_emoticon_panel_layout);
        viewPager = designer.getViewById(R.id.activity_main_emoticon_viewpager);
        emoticonImageViews = new ArrayList<>();
    }

    @Override
    protected void setWidgets() {
        editText.setCallBackSymbols("$", "#", "%");
        editText.setPicWrapPage("<", ">");
        editText.setEmoticonPattern("\\{:\\S+?\\}");
        editText.setRichTextAlignment(RichTextAlignment.BOTTOM);
        editText.setTopics("OFO", "美团");
        editText.setTextSize(TypedValue.COMPLEX_UNIT_PX, 100);
        editText.setEmoticonIconSize(100);
        editText.setPicMaxSize(screenW / 4, screenH / 4);
        editText.setEmoticonIconTable(FlymeBBSEmoticonIconTable.class, false);
        editText.setBackgroundColor(Color.LTGRAY);
        new XPxArea(editText).set(0, 0, XPxArea.MATCH, screenH * 0.5);

        new XPxArea(panelLayout).set(0, 0, XPxArea.MATCH, 300);

        ArrayList<View> panelViews = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            GridLayout panelLayout = new GridLayout(context);
            panelLayout.setColumnCount(7);
            panelLayout.setRowCount(4);

            ArrayList<Integer> resIds = editText.emoticonIconTable.getPageResIdsFromPage(i);
            for (int j = 0; j < resIds.size(); j++) {
                ImageView emoticonImageView = new ImageView(context);
                panelLayout.addView(emoticonImageView);
                emoticonImageViews.add(emoticonImageView);

                emoticonImageView.setImageResource(resIds.get(j));
                emoticonImageView.setTag(editText.emoticonIconTable.getEmotionIconNameFromResId(resIds.get(j)));
                new XPxArea(emoticonImageView).set(0, 0, screenW / 7);
            }
            panelViews.add(panelLayout);
        }
        viewPager.setAdapter(new ViewPagerAdapter(panelViews));
        new XPxArea(viewPager).set(0, 0, XPxArea.MATCH, XPxArea.WRAP);

        initializeFunctionLayout();
    }

    private void initializeFunctionLayout() {
        functionLayout.setOrientation(LinearLayout.HORIZONTAL);
        new XPxArea(functionLayout).set(0, 0, XPxArea.MATCH, XPxArea.WRAP);

        emoticonButton.setText("插入表情");
        picButton.setText("插入图片");
        topicButton.setText("插入话题");
    }
}
